Skip to content

gh-162350: Use AC vectorcall for cell.__new__ to speed up types.CellType - #162422

Open
tanloong wants to merge 4 commits into
python:mainfrom
tanloong:celltype_vectorcall
Open

tanloong wants to merge 4 commits into
python:mainfrom
tanloong:celltype_vectorcall

Conversation

@tanloong

@tanloong tanloong commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Replace hand-written cell_new with Argument Clinic + Vectorcall. Observed more than 10% performance boost.

Benchmark (default options):

+--------------------+--------+-----------------------+
| Benchmark          | main   | patch                 |
+====================+========+=======================+
| CellType(42)       | 122 ns | 91.7 ns: 1.33x faster |
+--------------------+--------+-----------------------+
| types.CellType(42) | 131 ns | 102 ns: 1.28x faster  |
+--------------------+--------+-----------------------+
| CellType()         | 103 ns | 83.9 ns: 1.22x faster |
+--------------------+--------+-----------------------+
| Geometric mean     | (ref)  | 1.28x faster          |
+--------------------+--------+-----------------------+
Details
import pyperf

SETUP = """
import sys
import types
from types import (CellType, FunctionType, MappingProxyType,
                   FrameLocalsProxyType)

def f(self):
    pass

class C:
    def m(self):
        pass

obj = C()
code = f.__code__
gns = globals()
d = {}
frame = sys._getframe()
"""

CASES = [
    ("CellType(42)", "CellType(42)"),
    ("types.CellType(42)", "types.CellType(42)"),
    ("CellType()", "CellType()"),
]


runner = pyperf.Runner()
for name, stmt in CASES:
    runner.timeit(name, stmt=stmt, setup=SETUP)

…s AC-generated signature 'cell(contents=None, /)' which is supported by 'inspect.signature'
Comment thread Objects/cellobject.c
@classmethod
cell.__new__ as cell_new
contents: object(c_default="NULL") = None

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this signature isn't technically the same as the old "defaults to None" and "has two overloads, 0 args and 1 arg" have some behavior differences... not certain how critical that is here...

To land this quickly I'd suggest using a @text_signature to keep the old signature. Adding optional group support to AC VC will likely be a bit (Although is looking like something I should prioritize)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to use @text_signature and revert previous test fix.

…t now has AC-generated signature 'cell(contents=None, /)' which is supported by 'inspect.signature'"

This reverts commit c2d3ea6.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants